/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import edu.syr.iis.web.model.AssignmentManager;
import edu.syr.iis.web.model.Conversation;
import edu.syr.iis.web.model.ConversationManager;
import edu.syr.iis.web.model.DisagreementMatrix;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;
import edu.syr.iis.web.util.Key;

/**
 * @author kinoue
 *
 */
public class ViewStatisticsController extends AbstractController
{
	UserManager userManager;
	ConversationManager conversationManager;
	AssignmentManager assignmentManager;
	
    
    /* (non-Javadoc)
     * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
	    HttpServletResponse response) throws Exception
    {
    	HashMap<String, Object> model = new HashMap<String, Object>();
		List <DisagreementMatrix> matrices = null;

    	// For the dropdown lists
    	List<User> users = userManager.getUsers();
    	
    	if (request.getParameter(Key.ANNO1_ID) != null
    			&& request.getParameter(Key.ANNO2_ID) != null)
    	{
    		User anno1 = userManager.getUser(Integer.parseInt(request.getParameter(Key.ANNO1_ID)));
    		User anno2 = userManager.getUser(Integer.parseInt(request.getParameter(Key.ANNO2_ID)));
    		
    		if (anno1 != null && anno2 != null)
    		{
    			double avgWD = conversationManager.getAverageWindowDiff(anno1, anno2);
    			double avgAvgWD = conversationManager.getAverageAverageWindowDiff(anno1, anno2);
    			
    			if (request.getParameter(Key.LEVEL) != null) 
    			{
    				int level = Integer.parseInt(request.getParameter(Key.LEVEL)); 
    			
    				matrices = new ArrayList<DisagreementMatrix>();
    				matrices.add(conversationManager.getCumulativeDisagreementMatrix(anno1, anno2, level));
    			
    			}
    			else
    			{
    				matrices = conversationManager.getCumulativeDisagreementMatrices(anno1, anno2);    				
    			}
    			model.put("matrices", matrices);	
    	    	model.put("avgWD", new Double(avgWD));
    	    	model.put("avgAvgWD", new Double(avgAvgWD));
    		}
    	}    		    	
    	return new ModelAndView("viewstatistics", model);
    }


    public void setAssignmentManager(AssignmentManager assignmentManager)
    {
        this.assignmentManager = assignmentManager;
    }


	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}


	public void setConversationManager(ConversationManager conversationManager) {
		this.conversationManager = conversationManager;
	}

}
